package org.zoomdev.zoom.asm;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

public class FieldBuilder {
    private final ClassBuilder classBuilder;
    private final String fieldName;
    private Type fieldType;

    public Type getFieldType(){
        return fieldType;
    }

    private Object initialValue;
    private int access = Opcodes.ACC_PRIVATE;
    private String signature;

    public FieldBuilder(ClassBuilder classBuilder, String fieldName) {
        this.classBuilder = classBuilder;
        this.fieldName = fieldName;
    }

    public FieldBuilder withAccess(int access) {
        this.access = access;
        return this;
    }

    public FieldBuilder withType(Class<?> type) {
        this.fieldType = Type.getType(type);
        return this;
    }

    public FieldBuilder withType(Type type) {
        this.fieldType = type;
        return this;
    }

    public FieldBuilder withInitialValue(Object value) {
        this.initialValue = value;
        return this;
    }

    public FieldBuilder withGenericSignature(String signature) {
        this.signature = signature;
        return this;
    }

    public void build(ClassWriter cw) {
        FieldVisitor fv = cw.visitField(access, fieldName,
                fieldType.getDescriptor(), signature, initialValue);

        if (fv != null) {
            fv.visitEnd();
        }
    }
}
